/*
 * js for update password in view file
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

$(function() {
 
    var
    np = $( "#new-password" ),
    rp = $( "#repeatenew-password" ),
    allFields = $( [] ).add( np ).add( rp ),
    tips = $( ".validateTips" );
      
      
    function updateTips( t ) {
        console.log(t);
        tips
        .text( t )
        .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 3000 );
        }, 500 );
    }
      
    function checkLength( o, n, min, max ) {
        if ( o.length > max || o.length < min ) {
         
            $( "#"+n ).addClass( "ui-state-error" );
            updateTips( "The minimum number of characters not met, it should " +
                min + " and " + max + "." );
            return false;
        } else {
            return true;
        }
    }
   
    function compairPassword( np ,rp ) {
        if ( np == rp ) {
            return true;
        } else {
            updateTips( "Re-entered password does not match ");
            return false;
        }
    }
   
    $( "#updatePassword-form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 400,
        modal: true,
        buttons: {
            "Change": function() {
                var 
                np = $( "#new-password" ).val(),
                rp = $( "#repeatenew-password" ).val(),
                user_id = $("#user_id").val();
                var bValid = true;
                allFields.removeClass( "ui-state-error" );
                bValid = bValid && checkLength( np, "new-password", 3, 16 );
                bValid = bValid && checkLength( rp, "repeatenew-password", 3, 16 );
                bValid = bValid && compairPassword(np,rp);
                if ( bValid ) {
                    $.ajax({
                        type : "POST",
                        url : '?r=user/AjaxUpdatePassword',
                        cache : false,
                        data : "uid=" + user_id+"&npsw="+np,
                        dataType : "html",
                        success : function(data) {
                            console.log(data);
                            if(data == 'valid'){
                                $('.alert-success').show();
                                setTimeout(function(){
                                    $(".alert-success").hide()
                                    }, 5000);   
                            }else{
                                $('.alert-error').show();
                                setTimeout(function(){
                                    $(".alert-error").hide()
                                    }, 5000);   
                            }
                           
                        }
                    });
                    $( this ).dialog( "close" );
                }
           
        
            },
             Back: function() {
                $( this ).dialog( "close" );
            }
            
           
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
 
    $(".change_psw").live("click", function(event){
        event.preventDefault();
        $( "#updatePassword-form" ).dialog( "open" );
    });

});
